package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Condition3
import com.abc.robot01.retrofit.bean.Customer
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch


class CustomersViewModel : ViewModel() {
    val TAG = "CustomersViewModel"
    val   customerPhone:MutableState<String> = mutableStateOf("")
    val   customerName:MutableState<String> = mutableStateOf("")
    //客户列表
    private val _customerList = MutableStateFlow<List<Customer>>(emptyList())
    val customerList: StateFlow<List<Customer>> = _customerList

    fun load() {
            request()
    }

     fun request(customerName: String = "", customerPhone: String = "") {
         viewModelScope.launch {
             val res = RetrofitService.instance.getCustomers(
                 accessToken = Robot.accessToken,
                 userId = Robot.userId,
                 companyId = Robot.companyId,
                 request = FetchDataRequest9(
                     condition = Condition3(
                         customerName = customerName,
                         customerPhone = customerPhone
                     )
                 )
             )
             Log.e(TAG, "客户列表:$res")
             if (res.code == 200) {
                 _customerList.value = res.data.list
             }
         }

    }
}
